﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Clothes.Items.ModuleData;
using ApplicationLibrary;
using System.Diagnostics;
using Clothes.Sales.Library;


namespace Clothes.Sales.Scrounger
{
    internal class MastersScrounger : ApplicationLibrary.DBManager.Scrounger
    {

        private Masters.Forms.Masters frmMaster;

        public MastersScrounger(iScrounger frm)
            : base(frm)
        {
            this.frmMaster = (Masters.Forms.Masters)frm;
        }

        public override void OnCustomizeToolbar(ToolStrip toolstrip)
        {
            guestForm.AddCustomButton("Invoice", Properties.Resources.CreateInvoice, Properties.Resources.B_Invoice, true);
            guestForm.AddCustomButton("BillOfLading", Properties.Resources.CreateBillOfLading, Properties.Resources.B_BillOfLading, false);
            guestForm.AddCustomButton("ReturnToCust", Properties.Resources.CreateReturnToCustomer, Properties.Resources.B_ReturnToCust, false);
            base.OnCustomizeToolbar(toolstrip);
        }

        public override bool OnEnableToolbarButtons(ToolStripButton sender, DBManager dbManager)
        {
            if (sender.Name == "Invoice")
                return dbManager.Count > 0;

            if (sender.Name == "BillOfLading")
                return dbManager.Count > 0;

            if (sender.Name == "ReturnToCust")
                return dbManager.Count > 0;

            return base.OnEnableToolbarButtons(sender, dbManager);
        }

        public override void OnCustomButtonClick(ToolStripButton button)
        {
            if (button.Name == "Invoice")
            {
                SaleDocCreator sC = new SaleDocCreator("Clothes.Clothes.Sales.Forms.InvoiceDoc");
                sC.AddNew();
                sC.SetHeader(GlobalInfo.CurrentDate, frmMaster.Customer.CounterNumericValue);
                LoadData(sC, frmMaster.Customer.CounterNumericValue);
            }

            if (button.Name == "BillOfLading")
            {
                SaleDocCreator sC = new SaleDocCreator("Clothes.Clothes.Sales.Forms.BillOfLadingDoc");
                sC.AddNew();
                sC.SetHeader(GlobalInfo.CurrentDate, frmMaster.Customer.CounterNumericValue);
                LoadData(sC, frmMaster.Customer.CounterNumericValue);
            }
        }


        private void LoadData(SaleDocCreator salecreator, int idCustomer)
        {
            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelect();
                qb.AddFrom(CL_Items.Name);

                SqlABParameter sqlP1 = new SqlABParameter("@p1", CL_Items.ReturnToCustomer);
                qb.AddCompare(CL_Items.ReturnToCustomer, sqlP1);
                sqlP1.Value = false;

                SqlABParameter sqlP2 = new SqlABParameter("@p2", CL_Items.Invoice);
                qb.AddCompare(CL_Items.Invoice, sqlP2);
                sqlP2.Value = false;

                SqlABParameter sqlP3 = new SqlABParameter("@p3", CL_Items.Customer);
                qb.AddCompare(CL_Items.Customer, sqlP3);
                sqlP3.Value = idCustomer;

                SqlABParameter sqlP4 = new SqlABParameter("@p4", CL_Items.Status);
                qb.AddCompare(CL_Items.Status, sqlP4);
                sqlP4.Value = ItemStatus.E_Sold.Int();

                scc.Parameters.AddRange(new List<SqlABParameter> { sqlP1, sqlP2, sqlP3, sqlP4 });

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();

                try
                {
                    while (dr.Read())
                    {
                        salecreator.AddRow(dr.GetValue<int>(CL_Items.ID));
                    }
                }
                catch (System.Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }
    }
}